package com.tre.jdevtemplateboot.service.impl;

import com.tre.jdevtemplateboot.common.context.BaseContext;
import com.tre.jdevtemplateboot.common.dto.ResponseResult;
import com.tre.jdevtemplateboot.domain.dto.SpAppFunctionMstDto;
import com.tre.jdevtemplateboot.domain.po.SpAppFunctionMst;
import com.tre.jdevtemplateboot.domain.vo.SpAppFunctionMstVO;
import com.tre.jdevtemplateboot.mapper.SpAppFunctionMstMapper;
import com.tre.jdevtemplateboot.service.ISpAppFunctionMstService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @author 熊一鸣
 * @since 2023-11-15
 */
@Service
public class SpAppFunctionMstServiceImpl extends ServiceImpl<SpAppFunctionMstMapper, SpAppFunctionMst> implements ISpAppFunctionMstService {

    @Override
    public ResponseResult findALl(String appId) {
        List<SpAppFunctionMst> pos = lambdaQuery()
                .eq(SpAppFunctionMst::getAppId,appId)
                .list();
        List<SpAppFunctionMstVO> vos = getVOListByPOList(pos);
        return ResponseResult.buildOK(vos);
    }

    @Override
    public ResponseResult saveFunction(SpAppFunctionMstDto dto) {
        SpAppFunctionMst spAppFunctionMst = new SpAppFunctionMst();
        BeanUtils.copyProperties(dto,spAppFunctionMst);
        spAppFunctionMst.setId(UUID.randomUUID().toString().replace("-","").getBytes());

        spAppFunctionMst.setCreateDate(LocalDateTime.now());
        spAppFunctionMst.setEditDate(LocalDateTime.now());
        spAppFunctionMst.setCreatePerson(BaseContext.getCurrentId());
        spAppFunctionMst.setEditPerson(BaseContext.getCurrentId());

        save(spAppFunctionMst);
        return ResponseResult.buildOK();
    }

    @Override
    public ResponseResult updateFunction(SpAppFunctionMstDto dto) {
        SpAppFunctionMst spAppFunctionMst = new SpAppFunctionMst();
        BeanUtils.copyProperties(dto,spAppFunctionMst);
        spAppFunctionMst.setEditDate(LocalDateTime.now());
        spAppFunctionMst.setEditPerson(BaseContext.getCurrentId());
        updateById(spAppFunctionMst);
        return ResponseResult.buildOK();
    }

    @Override
    public ResponseResult<SpAppFunctionMstVO> findById(String id) {
        SpAppFunctionMst function = lambdaQuery()
                .eq(SpAppFunctionMst::getId, id)
                .one();
        return ResponseResult.buildOK(function);
    }

    @Override
    public ResponseResult<List<SpAppFunctionMstVO>> findByParentId(String parentId) {
        List<SpAppFunctionMst> pos = lambdaQuery()
                .eq(SpAppFunctionMst::getParentId, parentId)
                .list();
        List<SpAppFunctionMstVO> vos = getVOListByPOList(pos);
        return ResponseResult.buildOK(vos);
    }

    public List<SpAppFunctionMstVO> getVOListByPOList(List<SpAppFunctionMst> pos){
        List<SpAppFunctionMstVO> vos = new ArrayList<>();
        for (SpAppFunctionMst po : pos) {
            SpAppFunctionMstVO vo = new SpAppFunctionMstVO();
            BeanUtils.copyProperties(po,vo);
            vos.add(vo);
        }
        return vos;
    }


}
